<?php
namespace App\repose;
use Image;
use App\project;
class projectrepose
{
    public function newProject($request){
          $request->user()->projects()->create([
                'name'=>$request->name,
                'thum'=>$this->upload($request)
            ]);
    }

    public function updateProject($request,$id){
        $projects = project::findOrFail($id);
        $projects->name = $request->name;
        if($request->hasFile('thum')){
            $projects->thum = $this->upload($request);
        }
        $projects->save();
    }

    public function upload($request){
        if($request->hasFile('thum')){
            $file = $request->thum;
            // laravel 自带的函数str_random（）随机返回一些字符
            $name = str_random(10).'.jpg';
            // 保存的路径拼上文件名
            $path = public_path().'/thum/'.$name;
            Image::make($file)->resize(200,100)->save($path);
            return $name;
        }
    }
}